﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;

namespace Marbles.Core.Content.Themes
{
    public class Theme
    {

        public string DirectoryPath { get { return _directoryname; } }
        public string Name { get { return _name; } }
        public string ApplicationDirectoryPath { get { return HttpContext.Current.Server.MapPath(DirectoryPath); } }
        public ThemeFile[] Files { get { return _files.ToArray(); } }

        private string _directoryname = string.Empty;
        private string _name = string.Empty;
        private List<ThemeFile> _files = null;

        public Theme()
        {
            _files = new List<ThemeFile>();
            _name = string.Empty;
            _directoryname = string.Empty;
        }

        public Theme(string Name)
            : this()
        {
            _name = Name;
            _directoryname = "/App_Themes/" + Name + "/";
            Refresh();
        }

        public void Refresh()
        {
            _files.Clear();
            foreach (string filename in System.IO.Directory.GetFiles(ApplicationDirectoryPath, "*.*"))
            {
                System.IO.FileInfo fileInfo = new System.IO.FileInfo(filename);
                ThemeFile file = new ThemeFile(fileInfo.FullName, _directoryname);
                _files.Add(file);
            }
            _files.Sort();
        }

        public static Theme CreateNew(string Name)
        {
            Theme t = new Theme(Name);
            if (!System.IO.Directory.Exists(t.ApplicationDirectoryPath)) { System.IO.Directory.CreateDirectory(t.ApplicationDirectoryPath); }
            return t;
        }
    }
}